<template>
  <section class="my-addaddress">
    <div class="content">
      <div class="flex item flex-middle">
        <div class="t">联系人</div>
        <input
          class="flex-input"
          v-model="contactsName"
          type="text"
          placeholder-style="color:#C1C7C9"
          placeholder="请输入收货人姓名"
        >
      </div>
      <div class="flex item flex-middle">
        <div class="t">联系电话</div>
        <input
          v-model="phone"
          class="flex-input"
          type="number"
          placeholder-style="color:#C1C7C9"
          :maxlength="11"
          placeholder="请输入收货人电话"
        >
      </div>
      <div class="flex item flex-middle">
        <div class="t">所在学校</div>
        <input
          v-model="schoolName"
          class="flex-input"
          type="text"
          placeholder-style="color:#C1C7C9"
          placeholder="选择输入所在学校名称"
        >
      </div>
      <div class="flex item flex-middle">
        <div class="t">详细地址</div>
        <input
          v-model="dtlAddr"
          class="flex-input"
          type="text"
          placeholder-style="color:#C1C7C9"
          placeholder="楼层房间号等信息"
        >
      </div>
      <!--
      <div class="flex item flex-middle">
        <div class="t">邮政编码</div>
        <input
          v-model="postalCode"
          type="number"
          class="flex-input"
          :maxlength="6"
          placeholder-style="color:#C1C7C9"
          placeholder="邮政编码"
        >
      </div>
      -->
      <div class="flex item flex-middle flex-between">
        <div
          class="t"
          style="width:120px"
        >设为默认地址</div>
        <van-switch
          custom-class="v-switch"
          size="20px"
          active-color="#3BB8FD"
          :checked="isDefault"
          @change="onChange"
        />
      </div>
    </div>
    <button
      class="add-new"
      @click="saveAddr"
    >保存</button>
  </section>
</template>
<script>
export default {
  data() {
    return {
      addrId: '',
      contactsName: "",//联系人
      phone: '',
      dtlAddr: '',//详细地址
      addr: '',//省市区
      postalCode: '',//邮政编码
      isDefault: false,
      area: '',//地区编号
      schoolName: '',
    };
  },
  onLoad(e) {
    if (e.type == 0) {
      wx.setNavigationBarTitle({
        title: "地址详情"
      });
      this.addrId = e.id;
      this.getAddrById();
    } else {
      this.contactsName = '';
      this.phone = '';
      this.addr = '';
      this.dtlAddr = '';
      this.isDefault = false;
      this.area = '';
      this.postalCode = '';
    }
  },
  methods: {
    routerTo(id) {
      wx.navigateTo({ url: "../orderdetail/index?id=" + id });
    },
    chooseSchool() {
      wx.showToast({
        title: "title", //提示的内容,
        icon: "none", //图标,
        duration: 2000, //延迟时间,
        mask: true, //显示透明蒙层，防止触摸穿透,
        success: res => { }
      });
    },
    //默认
    onChange(e) {
      this.isDefault = e.mp.detail;
    },
    //地址详情
    getAddrById() {
      this.$api.getAddrById({ addrId: this.addrId }).then(res => {
        this.addrId = res.data.addrId;
        this.contactsName = res.data.contactsName;
        this.phone = res.data.phone;
        this.addr = res.data.addr;
        this.postalCode = res.data.postalCode;
        this.schoolName = res.data.schoolName;
        this.dtlAddr = res.data.dtlAddr;
        this.isDefault = res.data.isDefault == 1 ? true : false;
      })
    },
    //新增修改地址
    saveAddr() {
      /* addrId	编号编号(存在则更新，不存在则保存)	是[long]		查看
      2	memberId	会员编号	是[string]
      3	contactsName	联系人	是[string]		查看
      4	phone	手机号	是[string]		查看
      5	schoolName	学校名称	是[string]		查看
      6	dtlAddr	详细地址	是[string]		查看
      7	isDefault	是否默认	是[int]		查看
      8	postalCode	邮政编码 */
      if(!(/^1[3456789]\d{9}$/.test(this.phone))){
          wx.showToast({
              title: '电话号码格式不正确',
              icon: 'none',
              duration: 2000,
              mask: false,
          });
          return;
      }
      if(!this.contactsName.trim()){
          wx.showToast({
              title: '联系人不能为空',
              icon: 'none',
              duration: 2000,
              mask: false,
          });
          return;
      }
      if(!this.dtlAddr){
          wx.showToast({
              title: '详细地址不能为空',
              icon: 'none',
              duration: 2000,
              mask: false,
          });
          return;
      }
      /*
      if(!this.postalCode){
          wx.showToast({
              title: '邮政编码不能为空',
              icon: 'none',
              duration: 2000,
              mask: false,
          });
          return;
      }
      if(this.postalCode.length != 6){
          wx.showToast({
              title: '邮政编码格式不正确',
              icon: 'none',
              duration: 2000,
              mask: false,
          });
          return;
      }
      */
      let params = {
        addrId: this.addrId || void 0,
        contactsName: this.contactsName,
        phone: this.phone,
        schoolName: this.schoolName,
        dtlAddr: this.dtlAddr,
        isDefault: this.isDefault,
        postalCode: this.postalCode,
      }
      this.$api.saveAddr(params).then(res => {
        if (res.code == 0) {
          wx.showToast({
            title: res.msg, //提示的内容,
            icon: "none", //图标,
            duration: 2000, //延迟时间,
            mask: true, //显示透明蒙层，防止触摸穿透,
            success: res => {
              setTimeout(() => {
                wx.navigateBack({
                  delta: 1 //返回的页面数，如果 delta 大于现有页面数，则返回到首页,
                });
              }, 1500)
            }
          });
        }
      });
    }
  }
};
</script>
<style lang="scss" scoped>
.my-addaddress {
  .content {
    margin: 15px;
    background: white;
    padding: 0 15px;
    .setbtn {
      color: #333;
      font-size: 14px;
      padding: 10px 15px;
    }
    .item {
      height: 44px;
      line-height: 44px;
      &:not(:last-child) {
        border-bottom: 1px solid #e9e9e9;
      }
      .t {
        width: 90px;
        font-size: 15px;
        font-family: PingFang;
        font-weight: 500;
        color: rgba(64, 68, 76, 1);
      }
      input {
        font-size: 12px;
        font-family: PingFang;
        font-weight: 500;
      }
    }
  }
  .add-new {
    width: 345px;
    height: 44px;
    line-height: 44px;
    background: rgba(59, 184, 253, 1);
    border-radius: 5px;
    position: fixed;
    bottom: 40px;
    left: 15px;
    font-size: 15px;
    font-family: PingFang;
    font-weight: 500;
    color: rgba(255, 255, 255, 1);
  }
}
.flex-input {
  flex: 1;
}
</style>
<style lang="scss">
page {
  background: #f7f7f7;
}
.v-switch {
  margin-top: 16px;
}
</style>
